//随手拍加载更多

var flagSsp = false;
var footH=$('.page-footer').height();

var $thelist=$('#thelist');
//拉下开始页数

var page = 2;

var isLoading = false;
//加载数据
function spList()
{
	  isLoading = true;
	  if(flagSsp == false)
	  {
	   $.get(url,{page:page},function(data)
		{
		    
		  if(data.result!=false){ //有数据
			  
			var ele = bulidSpNote(data.result,page);

			$thelist.append(ele);
			
			isLoading = false;
			setTimeout(lazyloadImg($('.lazy'+page)), 1000);
			
			page++; 
			
		}else { //数据为空
			if(flagSsp){
				addload.before('<p class="bottom-tips">— 已到最底部 —</p>').hide();
				flagSsp = true;
			}
		}
			
		  
	 },"JSON");
	  }
}

// 构建splist-html片段
function bulidSpNote(result,page)
{
    var i;

    var ele="";
    
	for(i=0;i<result.length; i++)
    {
	    var id = result[i].id;
	   var title = result[i].title;
	   var content = result[i].content;
	   var create_time = result[i].create_time;
	   var uavatar = result[i].uavatar;
	   var nickname = result[i].nickname;
	   var msgnum = result[i].msgnum;
	   var userid = result[i].userid;
	   var status = result[i].fans;
	   var pic = result[i].pic
	   var image = "";

	   var j;
	   
	   for(j=0;j<result[i].thumb.length; j++)
	   {
		   var thumb = result[i].thumb[j];
		   image+='<li class="lazy lazy'+page+' wxpicShow" data-original="'+thumb.thumb+'" data-pic="'+ thumb.pic +'"><img src="'+ img_url +'/o6.png" class="oimg"></li>';	
// 		   image+= result.thumb[i];
		   
	   }
	   


	   
	   ele+='<li class="ub cl-b-white clickable ssp-item url-href" data-url="/weixin/suipai/detail?id='+ id +'">'+
			'<img src="'+uavatar+'" class="ssp-tx tx-cir lazy">'+
		'<div class="ub-f1 ssp-cotent">'+
			'<div class="ssp-content-head">'+
				'<p class="cl-f-grey f16">'+title+'</p>';
		if(status == 1)
		{
			ele+='<a href="javascript:;" data-user="'+ userid +'" class="follow f12 cl-f-xlgrey follow'+userid+' active">已关注</a>';
		}
		else
		{
			ele+='<a href="javascript:;" data-user="'+ userid +'" class="follow f12 cl-f-xlgrey follow'+userid+'">+关注</a>';
		}
		ele+='</div>'+
			'<p class="ssp-content-article cl-f-grey">'+content+
//					'<span class="cl-f-orange">[淡奶油水果蛋糕]</span>'+content+
			'</p>'+
			'<ul class="ssp-content-img" data-pic="'+ pic +'">'+image+
			'</ul>'+
			'<div class="ssp-content-foot ub ub-pj">'+
				//'<p class="ssp-content-time cl-f-lgrey">2015-12-01<span>12:26</span></p>'+
			    '<p class="ssp-content-time cl-f-lgrey">'+create_time+'</p>'+
				'<div class="option-wrapper ub">'+
				'<div class="like ub ub-pc ub-ac">'+
		    	'<span class="icon-like"></span><p class="like-text cl-f-orange">0</p>'+
		    '</div>'+
					'<div class="comment ub ub-pc ub-ac">'+
				    	'<span class="icon-comment"></span><p class="comment-text cl-f-orange">'+msgnum+'</p>'+
				    '</div>'+
				'</div>'+
			'</div>'+
		'</div>'+
	'</li>';
	}
	return ele;
}
	
var addload = {
   	isTrue : $('#scroll-wrapper').index() !== -1,
   	addssp : function(){
   		if(this.isTrue) {
   			if($('#scroll-wrapper').height()<$(window).height()-45){
   				$('.add-loading').remove();
   				return false;
   			}
   			var addload = $('.add-loading');
			//var classList=$('#scroll-wrapper #thelist li').get(0).className;
    		$(window).on('scroll',function(){
    			
    			if(isLoading == false)
    			{
    				
	    				var scrollPos = $(window).scrollTop(),wrapperH = $('#scroll-wrapper').height();
	    				
	    				
	    				if(scrollPos > wrapperH - $(window).height() + footH ){
	    					
	    					spList();
	    					
	    				}
    			}
    		}); 
   		}
   	}
}

function loading(){
	if($('.oimg').length>0){
		if($('.oimg').get(0).complete){
			$(".loading-wrapper").hide();
			addload.addssp();
		}
	}
	else{
		$(".loading-wrapper").hide();
		addload.addssp();
	}
}

document.onreadystatechange = loading;

$(document).ready(function() {
	
	lazyloadImg($('.lazy'));
	//spList();
});
    
    
